package com.masterclient.app.modules.order.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.bean.address.AddressMessage;
import com.eoner.baselibrary.bean.order.ConfirmOrderData;
import com.eoner.baselibrary.bean.order.ConfirmStoreInfoBean;
import com.eoner.baselibrary.utils.IntentUtil;
import com.masterclient.app.R;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.base.MainViewHolder;

public class SubmitOrderAddressAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    ConfirmOrderData data;
    private String saleType;

    OnAddressClick onAddressClick;

    private boolean isGiving = false;

    public OnAddressClick getOnAddressClick() {
        return onAddressClick;
    }

    public void setOnAddressClick(OnAddressClick onAddressClick) {
        this.onAddressClick = onAddressClick;
    }


    public interface OnAddressClick{
        void OnItemClick();
    }

    public SubmitOrderAddressAdapter(Activity context, LayoutHelper layoutHelper, int count,String saleType,boolean isGiving) {
        this(context, layoutHelper, count, null,null,null,isGiving);
    }

    public SubmitOrderAddressAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, ConfirmOrderData data, String saleType,boolean isGiving) {
        this.mContext = context;
        this.isGiving = isGiving;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
        this.saleType = saleType;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        TextView address_user_name = holder.itemView.findViewById(R.id.address_user_name);
        TextView address = holder.itemView.findViewById(R.id.address);
        TextView tvAddressTip = holder.itemView.findViewById(R.id.tv_address_tip);
        TextView address_tel = holder.itemView.findViewById(R.id.address_tel);
        TextView choose_address_text = holder.itemView.findViewById(R.id.choose_address_text);
        View iv_address_arrow = holder.itemView.findViewById(R.id.iv_address_arrow);
        if (isGiving) {
            choose_address_text.setText("请填写受赠人（您要赠送的人）的地址！");
        } else {
            choose_address_text.setText("请选择收货地址");
        }
        if (data.isAddress_modify_enable()){
            tvAddressTip.setVisibility(View.GONE);
            iv_address_arrow.setVisibility(View.VISIBLE);
        }else {
            tvAddressTip.setVisibility(View.VISIBLE);
            iv_address_arrow.setVisibility(View.GONE);
        }
        View address_layout = holder.itemView.findViewById(R.id.address_layout);
        View item = holder.itemView.findViewById(R.id.item);
        View cross_border_tip_layout = holder.itemView.findViewById(R.id.cross_border_tip_layout);
        TextView flag = holder.itemView.findViewById(R.id.flag);
        View line = holder.itemView.findViewById(R.id.line);

        View layoutDealerInfo = holder.itemView.findViewById(R.id.layout_dealer_info);
        TextView tvDealerInfoName = holder.itemView.findViewById(R.id.tv_dealer_info_name);
        View layoutDealerPhone = holder.itemView.findViewById(R.id.layout_dealer_info_phone);
        TextView tvDealerPhone = holder.itemView.findViewById(R.id.tv_dealer_info_phone);
        ImageView ivDealerPhoneCall = holder.itemView.findViewById(R.id.iv_dealer_info_call);
        if (!TextUtils.isEmpty(saleType)&&"4".equals(saleType) && data.getStore_info()!=null){
            ConfirmStoreInfoBean storeInfoBean = data.getStore_info();
            layoutDealerInfo.setVisibility(View.VISIBLE);
            tvDealerInfoName.setText(storeInfoBean.getStore_name());
            layoutDealerPhone.setVisibility(TextUtils.isEmpty(storeInfoBean.getHide_contact_mobile())?View.GONE:View.VISIBLE);
            tvDealerPhone.setText(TextUtils.isEmpty(storeInfoBean.getHide_contact_mobile())
                    ?"":storeInfoBean.getHide_contact_mobile());
            ivDealerPhoneCall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ConfirmDialog dialog = new ConfirmDialog(mContext,"是否拨打电话",null,storeInfoBean.getContact_mobile(),"取消","确定","#FF6809");
                    dialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
                        @Override
                        public void cancel() {
                            dialog.dismiss();
                        }

                        @Override
                        public void confirm() {
                            IntentUtil.toPhoneApp(mContext,storeInfoBean.getContact_mobile());
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }
            });
        }

        if(data.isHas_cross_seller()){
            cross_border_tip_layout.setVisibility(View.VISIBLE);
            line.setVisibility(View.GONE);
        }else{
            cross_border_tip_layout.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(saleType)&&"4".equals(saleType)){
                line.setVisibility(View.GONE);
            }else {
                line.setVisibility(View.VISIBLE);
            }
        }

        AddressMessage addressMessage = data.getAddress();

        if (addressMessage != null && !TextUtils.isEmpty(addressMessage.getAddress())) {
            address_user_name.setText("收货人：" + addressMessage.getFull_name());
            address.setText(addressMessage.getProvince() + " " + addressMessage.getCity() + " " + addressMessage.getArea() + " " + addressMessage.getAddress());
            address_tel.setText(addressMessage.getMobile());
            choose_address_text.setVisibility(View.GONE);
            address_layout.setVisibility(View.VISIBLE);
        } else {
            choose_address_text.setVisibility(View.VISIBLE);
            address_layout.setVisibility(View.INVISIBLE);
        }

        if("1".equals(addressMessage.getIs_default())){
            flag.setVisibility(View.VISIBLE);
        }else{
            flag.setVisibility(View.GONE);
        }

        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onAddressClick != null){
                    onAddressClick.OnItemClick();
                }
            }
        });



    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 8;
    }
}
